package com.linkivr.action;

import java.io.IOException;
import java.util.List;

import javax.annotation.Resource;
import net.sf.ezmorph.bean.MorphDynaBean;
import net.sf.json.JSONArray;
import net.sf.json.JSONObject;

import org.springframework.stereotype.Component;

import com.linkivr.util.GL;
import com.linkivr.model.*;
import com.linkivr.service.*;


@Component("comportAction")
public class ComportAction extends MyAction {
	private static final long serialVersionUID = 1L;
	private ComportService service;
	private AppService appService;

	public ComportService getService() {
		return service;
	}

	@Resource
	public void setService(ComportService service) {
		this.service = service;
	}

	public AppService getAppService() {
		return appService;
	}

	@Resource
	public void setAppService(AppService appService) {
		this.appService = appService;
	}

	public String list() throws IOException {
		GL.info("user.list()函数  ");
		beginReqParam();

		List<T_comport> tabObjs = service.queryAll(start, limit);

		jsonString = "{totalProperty:" + service.queryCount() + ",root:[";
		int i = 0;
		for (T_comport to : tabObjs) {

			if (i != 0 && i != tabObjs.size())
				jsonString = jsonString + ",";
			i++;
			String t = "{id:'" + to.getId() + "',com_id:'" + to.getCom_id() + "',com_name:'" + to.getCom_name()
					+ "',com_baud:'" + to.getCom_baud() + "',com_parity:'" + to.getCom_parity() + "',com_data:'"
					+ to.getCom_data() + "',com_stop:'" + to.getCom_stop() + "',app_name:'" + to.getApp().getApp_name()
					+ "'}";
			jsonString = jsonString + t;
		}
		jsonString += "]}";
		out.write(jsonString);
		GL.info(jsonString);
		endReqParam();
		return null;
	}

	public String listAll() throws Exception {
		GL.info("listAll()函数  ");
		beginReqParam();
		JSONArray array = JSONArray.fromObject(service.queryAll());
		out.write(array.toString());
		endReqParam();
		return null;
	}

	public String query() throws IOException {
		GL.info("query()函数  ");
		String jsonString = "";
		beginReqParam();

		String p_id = request.getParameter("p_id");

		T_comport to = service.query(Integer.parseInt(p_id));
		GL.info("query()函数 ok p_id= " + p_id);
		jsonString = "{success: true ,data:{f_com_id:'" + to.getCom_id() + "',f_com_name:'" + to.getCom_name()
				+ "',f_com_baud:'" + to.getCom_baud() + "',f_com_parity:'" + to.getCom_parity() + "',f_com_data:'"
				+ to.getCom_data() + "',f_com_stop:'" + to.getCom_stop() + "',f_app_id:'" + to.getApp().getApp_id()
				+ "'}}";
		out.write(jsonString);
		GL.info(jsonString);
		endReqParam();
		return null;
	}

	public String add() throws IOException {
		GL.info("add()函数  ");
		String jsonString = "";
		beginReqParam();
		// 获取表达参数

		String f_com_id = request.getParameter("f_com_id");
		String f_com_name = request.getParameter("f_com_name");
		String f_com_baud = request.getParameter("f_com_baud");
		String f_com_parity = request.getParameter("f_com_parity");
		String f_com_data = request.getParameter("f_com_data");
		String f_com_stop = request.getParameter("f_com_stop");
		String f_app_id = request.getParameter("f_app_id");

		T_comport to = new T_comport();
		to.setCom_id(Integer.parseInt(f_com_id));
		to.setCom_name(f_com_name);
		to.setCom_baud(Integer.parseInt(f_com_baud));
		to.setCom_parity(Integer.parseInt(f_com_parity));
		to.setCom_data(Integer.parseInt(f_com_data));
		to.setCom_stop(Integer.parseInt(f_com_stop));

		T_app app = appService.query(Integer.parseInt(f_app_id));
		to.setApp(app);

		service.add(to);

		jsonString = "{success: true ,msg:'操作成功'}";
		out.write(jsonString);
		GL.info(jsonString);
		endReqParam();
		return null;
	}

	public String delete() throws IOException {
		GL.info("delete()函数  ");
		String jsonString = "";
		beginReqParam();
		// 获取表达参数

		String f_data = request.getParameter("f_data");
		JSONObject a = null;
		Object bean = null;
		JSONArray array = JSONArray.fromObject(f_data);
		Object[] objArray = array.toArray();
		for (Object obj : objArray) {
			a = JSONObject.fromObject(obj);
			bean = JSONObject.toBean(a);
			// 经过查看，发现 bean 为 MorphDynaBean 类型
			MorphDynaBean morphDynaBean = (MorphDynaBean) bean;
			String id = (String) morphDynaBean.get("id");
			service.delete(Integer.parseInt(id));

		}
		jsonString = "{success: true ,msg:'操作成功'}";
		out.write(jsonString);
		GL.info(jsonString);
		endReqParam();
		return null;
	}

	public String update() throws IOException {
		GL.info("update()函数  ");
		String jsonString = "";
		beginReqParam();
		// 获取表达参数

		String p_id = request.getParameter("p_id");

		String f_com_id = request.getParameter("f_com_id");
		String f_com_name = request.getParameter("f_com_name");
		String f_com_baud = request.getParameter("f_com_baud");
		String f_com_parity = request.getParameter("f_com_parity");
		String f_com_data = request.getParameter("f_com_data");
		String f_com_stop = request.getParameter("f_com_stop");
		String f_app_id = request.getParameter("f_app_id");

		T_comport to = service.query(Integer.parseInt(p_id));
		to.setCom_id(Integer.parseInt(f_com_id));
		to.setCom_name(f_com_name);
		to.setCom_baud(Integer.parseInt(f_com_baud));
		to.setCom_parity(Integer.parseInt(f_com_parity));
		to.setCom_data(Integer.parseInt(f_com_data));
		to.setCom_stop(Integer.parseInt(f_com_stop));

		T_app app = appService.query(Integer.parseInt(f_app_id));
		to.setApp(app);

		service.update(to);

		jsonString = "{success: true ,msg:'操作成功'}";
		out.write(jsonString);
		GL.info(jsonString);
		endReqParam();
		return null;
	}

}
